package com.yanqu.road.server.manager.workshop;

import com.yanqu.road.dao.impl.wingroom.WorkShopPoolDaoImpl;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.wingroom.workshop.WorkShopItem;
import com.yanqu.road.entity.wingroom.workshop.WorkShopPool;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.WorkShopCommonMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Date;
import java.util.List;

/**
 * @author ：zfr
 * @date ：2022/2/23 15:28
 */
public class WorkShopMgr extends TempMgr {
    private static WorkShopPool pool;
    private static WingRoomProto.WorkShopPoolTemp.Builder poolMsg;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        pool = new WorkShopPoolDaoImpl().getWorkShopPool(DateHelper.getTodayZeroTimeStamp());

        if (pool != null) {
            poolMsg = parseWorkShopPoolTemp(pool);
        }

        return true;
    }

    private static boolean isPoolRefreshTime() {
        if (pool == null) {
            return true;
        }
        int refreshTime = GameConfig.WINGROOM_WORKSHOP_REFRESH_TIME;
        int durDays = DateHelper.calc2DateTDOADays(new Date(pool.getRefreshTime()), new Date());
        return durDays > refreshTime;
    }

    public static synchronized void poolRefresh() {
        if (isPoolRefreshTime()) {
            refreshNewPool();
        }
    }

    private static void refreshNewPool() {
        if (pool == null) {
            pool = new WorkShopPool();
        }
        pool.setWorkShopItemList(WorkShopCommonMgr.newPool());
        pool.setRefreshTime(DateHelper.getTodayZeroTimeStamp());
        poolMsg = parseWorkShopPoolTemp(pool);
        notifyServerWorkShopPoolRefresh();
        new WorkShopPoolDaoImpl().addData(pool);
    }

    private static void notifyServerWorkShopPoolRefresh() {
        List<ServerInfo> openServerList = ServerListMgr.getChannelServerList(ConfigHelper.getInt("channelId"));
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_WING_ROOM_WORK_SHOP_POOL_SYNC, poolMsg);

        for (ServerInfo serverInfo : openServerList) {

            MessageHelper.sendPacket(serverInfo.getServerId(), 0, message);
        }
    }

    public static void syncServerWorkShopPool(long serverId) {
        poolRefresh();
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_WING_ROOM_WORK_SHOP_POOL_SYNC, poolMsg);
        MessageHelper.sendPacket(serverId, 0, message);
    }


    @Override
    public boolean stop() {
        return false;
    }

    private static WingRoomProto.WorkShopPoolTemp.Builder parseWorkShopPoolTemp(WorkShopPool pool) {
        WingRoomProto.WorkShopPoolTemp.Builder temp = WingRoomProto.WorkShopPoolTemp.newBuilder();
        temp.setRefreshTime(pool.getRefreshTime());
        for (WorkShopItem workShopItem : pool.getWorkShopItemList()) {
            temp.addWorkShopItem(parseWorkShopItemTemp(workShopItem));
        }
        return temp;
    }

    private static WingRoomProto.WorkShopItemTemp.Builder parseWorkShopItemTemp(WorkShopItem item) {
        WingRoomProto.WorkShopItemTemp.Builder temp = WingRoomProto.WorkShopItemTemp.newBuilder();
        temp.setFurnitureId(item.getFurnitureId());
        temp.setCount(item.getCount());
        return temp;
    }
}
